package com.bbs.listener;


import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import com.bbs.comm.SessionContext;
import com.bbs.model.User;


public class SessionListener implements HttpSessionListener{


	private  Logger log=Logger.getLogger(SessionListener.class);
   
	/**
	 * 注册SessionContext类，保存用户会话信息
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent se) {
    	
    	HttpSession session = se.getSession();
    	SessionContext context = new SessionContext();
    	//HttpServletRequest request = ServletActionContext.getRequest(); 
    	//context.setUserIP(request.getRemoteAddr());
    	User user=new User();
    	user.setUser_name("游客");
    	context.setUser(user);
    	context.regist(session);//注册session
    	log.info("a session create:"+session.getId());
    }

	/**
	 * 去掉SessionContext中的信息
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    @SuppressWarnings("unchecked")
	public void sessionDestroyed(HttpSessionEvent se) {
    	/*DateUtil util = new DateUtil();
    	HttpSession session = se.getSession();
    	String sessId=session.getId();
    	SessionContext<T_user> uContext = SessionContext.getSessionContext(session);
    	if(uContext!=null){
    		long temmin = 0;
    		try {
				temmin = util.toMin(uContext.getLogin_time());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			DaoFactory factory = DaoFactory.getInstance();
			TUserProxy proxy = new TUserProxy((TUserDao)factory.getDAO("TUser"),  new Authorization(true, true));
    		T_user user = uContext.getUser();
    		user.setU_usetime(user.getU_usetime()+(int)temmin);
			try {
				if(user.getU_state()!=1){
					proxy.updateTime(user);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			String account = uContext.getUser().getU_account();
    		log.info(account+" 在线时间 "+temmin+" 分钟");
    		
    		OnlineUser onlineUser = OnlineUser.getInstance();
    		onlineUser.remove(account);
    		uContext.regist();
    	}
    	log.info("a session destroyed :"+sessId);*/
    }
  
   
}
